﻿Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes

Partial Public Class CuentaControl 
  Inherits UserControl

  Public Enum CuentaType
    Suma
    Resta
    Multiplicacion
    Dividir
  End Enum

  Private mIscorrect As Boolean
  Public Property IsCorrect() As Boolean
    Get
      Return mIscorrect
    End Get
    Set(ByVal value As Boolean)
      mIscorrect = value
    End Set
  End Property

  Private mType As CuentaType
  Public Property Tipo() As CuentaType
    Get
      Return mType
    End Get
    Set(ByVal value As CuentaType)
      mType = value
    End Set
  End Property

  Private mID As String
  Public Property ID() As String
    Get
      Return mID
    End Get
    Set(ByVal value As String)
      mID = value
    End Set
  End Property

  Private mFileID As String
  Public Property FileID() As String
    Get
      Return mFileID
    End Get
    Set(ByVal value As String)
      mFileID = value
    End Set
  End Property

  Private mCuenta As String
  Public Property Cuenta() As String
    Get
      Return mCuenta
    End Get
    Set(ByVal value As String)
      mCuenta = value
      xCuenta.Text = value
    End Set
  End Property

  Private mResultado As Double
  Public Property Resultado() As Double
    Get
      Return mResultado
    End Get
    Set(ByVal value As Double)
      mResultado = value
      xResultado.Text = CStr(value)
    End Set
  End Property

  Private mResto As Double
  Public Property Resto() As Double
    Get
      Return mResto
    End Get
    Set(ByVal value As Double)
      mResto = value
      xResto.Text = CStr(value)
    End Set
  End Property

  Private mSolution As Double
  Public ReadOnly Property Solucion() As Double
    Get
      Return mSolution
    End Get
  End Property

  Private mArgumento1 As Double = -9999
  Public Property Argumento1() As Double
    Get
      Return mArgumento1
    End Get
    Set(ByVal value As Double)
      mArgumento1 = value
      ShowCuenta()
    End Set
  End Property

  Private mArgumento2 As Double = -9999
  Public Property Argumento2() As Double
    Get
      Return mArgumento2
    End Get
    Set(ByVal value As Double)
      mArgumento2 = value
      ShowCuenta()
      If mType = CuentaType.Dividir AndAlso mDcArgumento2 + mDcArgumento1 = 0 Then xResto.Visibility = Visibility.Visible
    End Set

  End Property

  Private mDcArgumento2 As Integer = 0
  Public Property DcArgumento2() As Integer
    Get
      Return mDcArgumento2
    End Get
    Set(ByVal value As Integer)
      mDcArgumento2 = value
    End Set
  End Property

  Private mDcArgumento1 As Integer = 0
  Public Property DcArgumento1() As Integer
    Get
      Return mDcArgumento1
    End Get
    Set(ByVal value As Integer)
      mDcArgumento1 = value
    End Set
  End Property

  Public Sub New()
    ' Required to initialize variables
    InitializeComponent()
  End Sub

  Public Sub New(ByVal sID As String, ByVal sType As CuentaType)
    ' Required to initialize variables
    InitializeComponent()
    mID = sID
    mType = sType
    xResto.Visibility = Visibility.Collapsed
    xSolution.Visibility = Visibility.Collapsed
  End Sub



  Private Sub ShowCuenta()
    Try
      If mArgumento1 = -9999 OrElse mArgumento2 = -9999 Then Exit Try
      xResultado.Text = String.Empty
      xResto.Text = String.Empty
      Select Case mType
        Case CuentaType.Suma
          xCuenta.Text = String.Format("{0} + {1}", mArgumento1, mArgumento2).ToString
          mSolution = Math.Round(mArgumento1, mDcArgumento1) + Math.Round(mArgumento2, mDcArgumento2)
          If mDcArgumento1 > mDcArgumento2 Then
            mSolution = Math.Round(mSolution, mDcArgumento1)
          Else
            mSolution = Math.Round(mSolution, mDcArgumento2)
          End If
          xSolution.Text = mSolution
        Case CuentaType.Resta
          xCuenta.Text = String.Format("{0} - {1}", mArgumento1, mArgumento2).ToString
          mSolution = Math.Round(mArgumento1, mDcArgumento1) - Math.Round(mArgumento2, mDcArgumento2)
          If mDcArgumento1 > mDcArgumento2 Then
            mSolution = Math.Round(mSolution, mDcArgumento1)
          Else
            mSolution = Math.Round(mSolution, mDcArgumento2)
          End If
          xSolution.Text = mSolution
        Case CuentaType.Multiplicacion
          xCuenta.Text = String.Format("{0} * {1}", mArgumento1, mArgumento2).ToString
          mSolution = Math.Round(Math.Round(mArgumento1, mDcArgumento1) * Math.Round(mArgumento2, mDcArgumento2), mDcArgumento1 + mDcArgumento2)
          xSolution.Text = mSolution
        Case CuentaType.Dividir
          xCuenta.Text = String.Format("{0} / {1}", mArgumento1, mArgumento2).ToString
          mSolution = Math.Round(mArgumento1, mDcArgumento1) / Math.Round(mArgumento2, mDcArgumento2)
          If (mDcArgumento1 + mDcArgumento2) = 0 Then
            xSolution.Text = String.Format("{0} con {1}", Int(mSolution), Math.Round((mSolution - Int(mSolution)) * mArgumento2))
          Else
            mSolution = Math.Round(mSolution, (mDcArgumento1 + mDcArgumento2))
            xSolution.Text = mSolution
          End If


      End Select

      Persist(String.Format("{0} = {1}. {2}", xCuenta.Text, xSolution.Text, vbCrLf), mFileID)


    Catch ex As Exception
      LoggingErr(ex)
    End Try

  End Sub

  Public Sub ShowSolucion()
    xSolution.Visibility = Visibility.Visible
  End Sub

  Public Sub HideSolucion()
    xSolution.Visibility = Visibility.Collapsed
  End Sub

  Private Sub xResultado_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.LostFocus
    Try
      If xResultado.Text.Length = 0 Then
        VisualStateManager.GoToState(Me, "Normal", True)
        Exit Try
      End If

      Dim res As Double
      Try
        res = CDbl(xResultado.Text)
      Catch ex As Exception
        VisualStateManager.GoToState(Me, "InCorrecto", True)
        mIscorrect = False
        Exit Sub
      End Try

      Select Case mType
        Case CuentaType.Suma, CuentaType.Resta, CuentaType.Multiplicacion
          'If xResultado.Text.Length = 0 Then VisualStateManager.GoToStateMe, "Normal", True)
          If res = mSolution Then
            VisualStateManager.GoToState(Me, "Correcto", True)
            mIscorrect = True
          Else
            VisualStateManager.GoToState(Me, "InCorrecto", True)
            mIscorrect = False
          End If
        Case CuentaType.Dividir
          If (mDcArgumento1 + mDcArgumento2) = 0 Then

            If res = CInt(Math.Round(mSolution)) AndAlso CInt(xResto.Text) = Math.Round(((mSolution - Math.Round(mSolution)) * mArgumento2)) Then
              VisualStateManager.GoToState(Me, "Correcto", True)
              mIscorrect = True
            Else
              VisualStateManager.GoToState(Me, "InCorrecto", True)
              mIscorrect = False
            End If
          Else
            If res = mSolution Then
              VisualStateManager.GoToState(Me, "Correcto", True)
              mIscorrect = True
            Else
              VisualStateManager.GoToState(Me, "InCorrecto", True)
              mIscorrect = False
            End If
          End If
      End Select

    Catch ex As Exception
      LoggingErr(ex)
    End Try
  End Sub


End Class
